import os
import time

from selenium.webdriver.common.by import By                           # 到入selenium的By

from base.events import Event                                         # 导入基础的操作事件
from handle.handle_yaml import ui_yaml_handle                            # 导入yaml 格式的用例
from handle.handle_json import handle_json                            # 导入json  格式的用例
from handle.handle_path import CASE_FOR_YAML_DIR                      # 导入用例存放的路径
from handle.handle_log import log                                     # 导入日志模块
from utils.scanElMessage import scanElMessage


def select_method(method):
    """
    定位方式
    """
    #  xpath  定位
    if method == 'xpath':
        return By.XPATH
    #  css_selector  定位
    elif method == 'css_selector':
        return By.CSS_SELECTOR
    #  link_text  定位
    elif method == 'link_text':
        return By.LINK_TEXT
    #  id  定位
    elif method == 'id':
        return By.ID
    # name  定位
    elif method == 'name':
        return By.NAME
    # class_name  定位
    elif method == 'class_name':
        return By.CLASS_NAME
    # tag_name  定位
    elif method == 'tag_name':
        return By.TAG_NAME
    # partial_link_text 定位
    elif method == 'partial_link_text':
        return By.PARTIAL_LINK_TEXT
    # 定位方式异常
    else:
        raise Exception('定位方式异常：当前版本不支持该定位方式')




def execute_demo(case:dict, data_record:dict = None, driver=None, ):
    event = Event(driver)
    case_name = [k for k in case.keys() if k.startswith('case')][0]
    step_record = {}
    step_record.update({
        'case_name': case[case_name]["name"]
    })

    steps = sorted([step for step in case[case_name].keys() if step.startswith('step')])
    for step in steps:
        if "url" in case[case_name][step]:
            try:
                event.open_url(case[case_name][step]["url"])
            except Exception as e:
                step_record.update({
                    f"{step}": {
                        "name": case[case_name][step]["name"],
                        "msg": "操作失败，失败信息{}".format(e)
                    }
                }
                )
                log.error(e)
            else:
                step_record.update({
                    f"{step}": {
                        "name": case[case_name][step]["name"],
                        "msg": "操作成功"
                    }
                })
            scan_el_manage = scanElMessage(driver)
            if scan_el_manage:
                step_record.update({
                    f"{step}": {
                        "name": case[case_name][step]["name"],
                        "msg": scan_el_manage
                    }
                })
        if "ele" in case[case_name][step]:
            step_element = case[case_name][step]["ele"]
            step_method = case[case_name][step]["positioning_method"]
            step_event = case[case_name][step]["event"]
            if step_event["key"] == "send_keys":
                try:
                    time.sleep(2)
                    event.send_keys_event(step_event["value"], step_method, step_element)
                except Exception as e:
                    step_record.update(
                        {
                            f"{step}": {
                                "name": case[case_name][step]["name"],
                                "s": "操作失败，失败信息{}".format(e)
                            }
                        }
                    )
                    log.error(e)
                else:
                    step_record.update(
                        {
                            f"{step}": {
                                "name": case[case_name][step]["name"],
                                "msg": "操作成功"
                            }
                        }
                    )
            elif step_event["key"] == "click":
                try:
                    time.sleep(2)
                    event.click_event(step_method, step_element)
                except Exception as e:
                    step_record.update(
                        {
                            f"{step}": {
                                "name": case[case_name][step]["name"],
                                "msg": "操作失败，失败信息{}".format(e)
                            }
                        }
                    )
                    log.error(e)
                else:
                    step_record.update(
                        {
                            f"{step}": {
                                "name": case[case_name][step]["name"],
                                "msg": "操作成功"
                            }
                        }
                    )
                # 扫描消息提示事件
                scan_el_manage = scanElMessage(driver)
                if scan_el_manage:
                    step_record.update({
                        f"{step}": {
                            "name": case[case_name][step]["name"],
                            "msg": scan_el_manage
                        }
                    })

            # 其他的事件在这下边加，如上

            else:
                pass
    data_record[case_name] = step_record


